package org.opensource.webflux.quick;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;

import static org.springframework.web.reactive.function.server.ServerResponse.ok;

@Slf4j
@Component
public class TestHandler {

    /*@SneakyThrows
    @GetMapping("/{tb}/{method}")
    public Flux<Response> flux(@PathVariable("tb") String tb, @PathVariable("method") String method) {
        return Flux.just(Response.newInstance(tb), Response.newInstance(method));
    }

    @SneakyThrows
    @GetMapping("/xxx/{*path}")
    public Mono other(@PathVariable("path") String path) {
        return Mono.just(path);
    }*/

    public Mono<ServerResponse> test(ServerRequest serverRequest) {
        return ok().contentType(MediaType.TEXT_EVENT_STREAM).body(  // 1
                Flux.interval(Duration.ofSeconds(1)).   // 2
                        map(l -> new SimpleDateFormat("HH:mm:ss").format(new Date())),
                String.class);
    }

    @Data
    public static class Response {
        private String str;

        public static Response newInstance(String str) {
            Response it = new Response();
            it.setStr(str);
            return it;
        }
    }

}
